﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VideoClassLibrary.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace VideoClassLibrary.BusinessLogicLayer
{
   public class RecruitMessageModule
    {
        #region  成员方法
        /// <summary>
        /// 修改新闻状态
        /// </summary>
        /// <param name="schoolId">所属学校</param>
        /// <param name="IsPublished">状态</param>
        /// <param name="Nid">新闻id</param>
        /// <returns></returns>
       public DataSet UpdateRecruitIsPublished(int schoolId, int IsPublished, int Nid)
        {
            string sql = "update V_RecruitStudent set  IsPulished=@IsPulished where  ID=@rid and schoolid=@schoolId";
            Database db = VDataBase.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(sql.ToString());
            db.AddInParameter(dbCommand, "rid", DbType.Int32, Nid);
            db.AddInParameter(dbCommand, "IsPulished", DbType.Int32, IsPublished);
            db.AddInParameter(dbCommand, "schoolid", DbType.Int32, schoolId);
            DataSet ds = db.ExecuteDataSet(dbCommand);
            return ds;
        }

        /// <summary>
        /// 增加一条招商简章
        /// </summary>
       public int AddRecruitMessage(V_RecruitStudent model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into V_RecruitStudent(");
            strSql.Append("RecruitTitle,RecruitContent,Author,SchoolId,IsPulished,CreateTime)");

            strSql.Append(" values (");
            strSql.Append("@RecruitTitle,@RecruitContent,@Author,@SchoolId,@IsPulished,@CreateTime)");
            strSql.Append(";select @@IDENTITY");
            Database db = VDataBase.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecruitTitle", DbType.String, model.RecruitTitle);
            db.AddInParameter(dbCommand, "RecruitContent", DbType.String, model.RecruitContent);
            db.AddInParameter(dbCommand, "Author", DbType.String, model.Author);
            db.AddInParameter(dbCommand, "SchoolId", DbType.Int32, model.SchoolId);
            db.AddInParameter(dbCommand, "IsPulished", DbType.Int32, model.IsPulished);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, model.CreateTime);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新招生简章
        /// </summary>
       public void UpdateRecruitMessage(V_RecruitStudent model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update V_RecruitStudent set ");
            strSql.Append("RecruitTitle=@RecruitTitle,");
            strSql.Append("RecruitContent=@RecruitContent,");
            strSql.Append("SchoolId=@SchoolId,");
            strSql.Append("IsPulished=@IsPulished");
            strSql.Append(" where ID=@ID ");
            Database db = VDataBase.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, model.ID);
            db.AddInParameter(dbCommand, "RecruitTitle", DbType.String, model.RecruitTitle);
            db.AddInParameter(dbCommand, "RecruitContent", DbType.String, model.RecruitContent);
            db.AddInParameter(dbCommand, "SchoolId", DbType.Int32, model.SchoolId);
            db.AddInParameter(dbCommand, "IsPulished", DbType.Int32, model.IsPulished);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除招生简章
        /// </summary>
       public void DeleteRecruitMessage(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from V_RecruitStudent ");
            strSql.Append(" where ID=@ID ");
            Database db = VDataBase.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            db.ExecuteNonQuery(dbCommand);

        }

       /// <summary>
       /// 获取学校的招生简章
       /// </summary>
       /// <param name="schoolId"></param>
       /// <returns></returns>
       public DataSet GetRecruitMessageList(string schoolId,string whereSql)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("select ID,RecruitTitle,RecruitContent,Author,SchoolId,IsPulished,CreateTime ");
           strSql.Append(" FROM V_RecruitStudent where SchoolId="+schoolId);
           if (whereSql.Trim() != "")
           {
               strSql.Append(whereSql);
           }
           Database db = VDataBase.CreateDatabase();
           return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
       }
       /// <summary>
       /// 学校ID查询招生简章
       /// </summary>
       /// <param name="sid">招生简章</param>
       /// <returns></returns>
       public DataSet GetRecruitBysSchoolId(int sid)
       {
           string sql = "  select top(5) ID,RecruitTitle,RecruitContent,Author,SchoolId,IsPulished,CreateTime FROM V_RecruitStudent where Schoolid=@SchoolID order by ID desc";
           Database db = VDataBase.CreateDatabase();
           DbCommand command = db.GetSqlStringCommand(sql.ToString());
           db.AddInParameter(command, "SchoolID", DbType.String, sid);
           return db.ExecuteDataSet(command);
       }

       /// <summary>
       /// 显示招生简章内容
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public DataSet GetRecruiByid( string id)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("select ID,RecruitTitle,RecruitContent,Author,SchoolId,IsPulished,CreateTime ");
           strSql.Append(" FROM V_RecruitStudent where ID=" + id);
           Database db = VDataBase.CreateDatabase();
           return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
       }

        #endregion  成员方法
    }
}
